/**
 * Author(s): xuming.Wong <xuming.Wong@gmail.com>
 */
#include "NodeInfoBox.h"
#include "DiagramView.h"

namespace QSThink
{
	NodeInfoBox::NodeInfoBox( )
		: _node(0)
	{
		//setFlag(ItemIsSelectable);//ItemIsMovable
		//setFlag(ItemSendsGeometryChanges);
		setCacheMode(DeviceCoordinateCache);

		setZValue(1);
	}
	
	
	QRectF NodeInfoBox::boundingRect() const
	{
		return QRectF(0,0,256,128);
	}


	QPainterPath NodeInfoBox::shape() const
	{
		QPainterPath path;
		path.addRect(boundingRect());
		return path;
	}

	void NodeInfoBox::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
	{
		return;
		//draw background
		painter->setBrush(Qt::white);
		painter->setPen(QPen(Qt::black,0));
		painter->drawRect(0,0,256,128);

		if(!_node ){
			return;
		}

		Transaction* trans=_node->getTransaction();
		Transaction::Type type=trans->getType();

		switch (type)
		{
		case Transaction::Transaction_Single:
			{
			TransactionSingle* t=(TransactionSingle*)trans;
			paintSingle(painter,t);
			break;
			}
		case Transaction::Transaction_2Minutes:
			{
				Transaction2Minutes* t=(Transaction2Minutes*)trans;
				paint2Minutes(painter,t);
				break;
			}
		case Transaction::Transaction_30Minutes:
			{
				
				break;
			}
		case Transaction::Transaction_1Hour:
			{
				
				break;
			}
		case Transaction::Transaction_1Day:
			{
				Transaction1Day* t=(Transaction1Day*)trans;
				paint1Day(painter,t);
				break;
			}

		}
		
	}

	QVariant NodeInfoBox::itemChange( GraphicsItemChange change, const QVariant &value )
	{
		return QGraphicsItem::itemChange(change, value);
	}

	void NodeInfoBox::mousePressEvent( QGraphicsSceneMouseEvent *evt )
	{
		update();
		QGraphicsItem::mousePressEvent(evt);

	}

	void NodeInfoBox::mouseReleaseEvent( QGraphicsSceneMouseEvent *evt )
	{
		update();
		QGraphicsItem::mouseReleaseEvent(evt);

	}

	void NodeInfoBox::setNode( DiagramNode* node )
	{
		_node=node;
		update();
	}

	void NodeInfoBox::paintSingle( QPainter * painter, TransactionSingle* t )
	{
		
	}

	void NodeInfoBox::paint2Minutes( QPainter * painter, Transaction2Minutes* t )
	{
		
	}

	void NodeInfoBox::paint1Day( QPainter * painter, Transaction1Day* t )
	{
		int day=t->day();
		float balancePrice=(float)t->balancePrice()/100.0f;
		float highPrice=(float)t->highPrice()/100.0f;
		float lowPrice=(float)t->lowPrice()/100.0f;
		float openPrice=(float)t->openPrice()/100.0f;
		float closePrice=(float)t->closePrice()/100.0f;
		int volume=t->volume();
		int amount=t->amount();
		
		painter->setPen(Qt::black);
		painter->setFont(QFont("Arial", 10));


		QString dateStr=Utilities::getDateStrByDay(day);

		int row=0;

		row++;
		row++;
		painter->drawText(24,12*row, "date:");
		painter->drawText(128,12*row,dateStr);

		row++;
		painter->drawText(24,12*row, "price:");
		painter->drawText(128,12*row,QString::number(balancePrice));

		row++;
		painter->drawText(24,12*row, "high:");
		painter->drawText(128,12*row,QString::number(highPrice));

		row++;
		painter->drawText(24,12*row, "low:");
		painter->drawText(128,12*row,QString::number(lowPrice));

		row++;
		painter->drawText(24,12*row, "open:");
		painter->drawText(128,12*row,QString::number(openPrice));

		row++;
		painter->drawText(24,12*row, "close:");
		painter->drawText(128,12*row,QString::number(closePrice));


		row++;
		painter->drawText(24,12*row, "volume:");
		painter->drawText(128,12*row,QString::number(volume));

		row++;
		painter->drawText(24,12*row, "amount:");
		painter->drawText(128,12*row,QString::number(amount));

	}


}
